from django.shortcuts import render
from django.http import HttpResponseRedirect,Http404
from django.urls import reverse
from django.contrib.auth.decorators import login_required

from .models import Topic,Blog,Comment
from .forms import TopicForm,BlogForm,CommentForm

import markdown


def index(request):
	return render(request,'blogs/index.html')

@login_required
def topics(request):
	topics = Topic.objects.order_by('date_added')
	context = {'topics':topics}
	return render(request,'blogs/topics.html',context)

@login_required	
def topic(request,topic_id):
	topic = Topic.objects.get(id=topic_id)
	blogs = topic.blog_set.order_by('-date_added')
	context = {'topic':topic,'blogs':blogs}
	return render(request,'blogs/topic.html',context)
	
@login_required	
def blog(request,blog_id):
	blog = Blog.objects.get(id=blog_id)
	blog.text = markdown.markdown(
		blog.text,
		extensions=[
			'markdown.extensions.extra',
			'markdown.extensions.codehilite',
			'markdown.extensions.toc',
		]
	)
	comments = blog.comment_set.order_by('date_added')
	topic = blog.topic
	if request.method != 'POST':
		form = CommentForm()

	context = {'form':form,'blog':blog,'topic':topic,'comments':comments}
	return render(request,'blogs/blog.html',context)

@login_required
def new_topic(request):
	if not request.user.is_superuser:
		raise Http404
	if request.method != 'POST':
		form = TopicForm()
	else:
		form = TopicForm(request.POST)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect(reverse('blogs:topics'))
	context = {'form':form}
	return render(request,'blogs/new_topic.html',context)

@login_required	
def new_blog(request,topic_id):
	topic = Topic.objects.get(id=topic_id)
	if request.method != 'POST':
		form = BlogForm()
	else:
		form = BlogForm(request.POST)
		if form.is_valid():
			new_blog = form.save(commit=False)
			new_blog.Owner = request.user
			new_blog.topic = topic
			new_blog.save()
			return HttpResponseRedirect(reverse('blogs:topic',
										args=[topic_id]))
	context = {'topic':topic,'form':form}
	return render(request,'blogs/new_blog.html',context)
	
@login_required		
def comment(request,blog_id):
	blog = Blog.objects.get(id=blog_id)
	if request.method != 'POST':
		raise Http404
	else:
		form = CommentForm(request.POST)
		if form.is_valid():
			new_form = form.save(commit=False)
			new_form.Owner = request.user
			new_form.blog = blog
			new_form.save()
			return HttpResponseRedirect(reverse('blogs:blog',
										args=[blog_id]))
	# ~ context = {'blog':blog,'form':form}
	# ~ return render(request,'blogs/blog.html')

@login_required	
def edit_blog(request,blog_id):
	blog = Blog.objects.get(id=blog_id)
	if request.user != blog.Owner:
		raise Http404
	topic = blog.topic
	if request.method != 'POST':
		form = BlogForm(instance=blog)
	else:
		form = BlogForm(instance=blog,data=request.POST)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect(reverse('blogs:blog',
										args=[blog_id]))
	context = {'topic':topic,'blog':blog,'form':form}
	return render(request,'blogs/edit_blog.html',context)
	
@login_required		
def delete_blog(request,blog_id):
	blog = Blog.objects.get(id=blog_id)
	topic = blog.topic
	if request.user != blog.Owner and request.user != topic.Owner:
		raise Http404
	blog.delete()
	return HttpResponseRedirect(reverse('blogs:topic',args=[topic.id]))

@login_required	
def delete_comment(request,comment_id):
	comment = Comment.objects.get(id=comment_id)
	blog = comment.blog
	if not (request.user == comment.Owner or request.user.is_superuser or blog.Owner == user):
		raise Http404
	if request.method != 'POST':
		form = CommentForm()
	comment.delete()
	comments = blog.comment_set.order_by('date_added')
	return HttpResponseRedirect(reverse('blogs:blog',args=[blog.id]))
